<?php


namespace App\Http\Controllers\Admin;


use App\Model\Admin;
use App\Tool\Json;
use App\Tool\Password;
use App\Tool\Token;


class LoginController extends BaseController
{


    /**
     * 登录接口
     * Create by Peter
     * 2019/08/14 09:55:32
     * Email:904801074@qq.com
     * @return false|string
     * @throws \Exception
     */
    function login()
    {

        $post = request()->input();

        $username = $post['username']??null;

        $password = $post['password']??null;

        $re = Admin::where('username', $username)
            ->where('password', Password::getPassword($password))
            ->first();



        if ($re){

            $re->last_login_time=time();

            $re->save();

//            $re->save(['last_login_time']);

            return Json::code(1,'登录成功！',Token::createToken($re->id));
        }

        return Json::code(2,'登录失败！');


    }


    function add()
    {

        echo Token::createToken();
    }


    private function createApiToken()
    {


    }

}
